import java.util.Arrays;
import java.util.Stack;

public class Test {

    /**
     * 栈中的数的匹配
     */
    public boolean IsPopOrder(int[] pushA, int[] popA) {
        if(pushA.length == 0 || popA.length == 0) {
            return false;
        }

        Stack<Integer> stack = new Stack<>();
        int j = 0;

        for (int i = 0; i < pushA.length; i++){
            int val = pushA[i];
            stack.push(val);

            while (!stack.empty() && j < popA.length && stack.peek() == popA[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    /**
     * 匹配括号
     * @param s
     * @return
     */
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //如果是左括号 就入栈
            if (ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            } else {
                if (stack.empty()) {
                    //遇到右括号 但是栈中没有左括号
                    return false;
                }
                char c = stack.peek();//将栈中的东西拿出来匹配
                if (c == '(' && ch == ')' || c == '[' && ch == ']' || c == '{' && ch == '}') {
                    stack.pop();//当前两个是匹配的 就弹出栈中存在的左括号
                } else {
                    return false;
                }
            }
        }
        //如果string的字符都走完了 但是栈中还有元素 就说明不匹配 就不行
        if (!stack.empty()) {
            return false;
        }
        //都匹配完成 返回true
        return true;
    }

    /**
     * 中缀表达式 逆波兰表达式
     * @param tokens
     * @return
     */
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++){
            String x = tokens[i];
            if (!isOpertion(x)) {
                stack.push(Integer.parseInt(x));
            }else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(x) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private boolean isOpertion(String x) {
        if (x.equals("+") || x.equals("-") || x.equals("*") || x.equals("/")){
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        MyStack<Integer> stack = new MyStack<>();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);
        int x = stack.pop();
        System.out.println(x);;
        x = stack.pop();
        System.out.println(x);
        x = stack.peek();
        System.out.println(x);
        x = stack.pop();
        System.out.println(x);

        boolean flg = stack.empty();
        System.out.println(flg);
        stack.pop();
        flg = stack.empty();
        System.out.println(flg);
    }

    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);
        int x = stack.pop();
        System.out.println(x);;
        x = stack.pop();
        System.out.println(x);
        x = stack.peek();
        System.out.println(x);
        x = stack.pop();
        System.out.println(x);

        boolean flg = stack.empty();
        System.out.println(flg);
        stack.pop();
        flg = stack.empty();
        System.out.println(flg);
    }
}
